using UnityEngine;

public class ButBuyProductCoinsByGold : MonoBehaviour
{
	[SerializeField]
	private ButBuyProductCoinsByGoldConfirm confirm;

	[SerializeField]
	private CoinShopManager coinShopManager;

	public InAppProduct curProductItem;

	private void Start()
	{
		if (curProductItem == null)
		{
			FindInParent(base.transform);
		}
	}

	private void OnClick()
	{
		confirm.Show(curProductItem.kolAddMoneySave, curProductItem.alternativePriceSave, BuyCoins);
	}

	private void BuyCoins()
	{
		if (settings.CathedWarnings >= (int)curProductItem.alternativePriceSave)
		{
			settings.CathedWarnings -= curProductItem.alternativePriceSave;
			settings.ShowSpecialLabelWithAnimation(curProductItem.kolAddMoneySave, base.transform.position, false);
			if (!curProductItem.promoEnabled)
			{
				return;
			}
			InAppPromoController.InAppPromo promoById = InAppPromoController.instance.inAppPromos.GetPromoById(curProductItem.idItems);
			if (!promoById.promoBonuses.GiveAlternativeBonuses())
			{
				if (promoById.promoBonuses.cashAmount > 0)
				{
					CoroutineRunner.Instance.StartCoroutine(settings.ShowSpecialLabelWithAnimationDelayd(promoById.promoBonuses.cashAmount, InAppShopUI.instance.promoRewardCotrollerUI.SetRewardUI("cash", promoById.promoBonuses.cashAmount), false));
				}
				if (promoById.promoBonuses.goldAmount > 0)
				{
					CoroutineRunner.Instance.StartCoroutine(settings.ShowLabelWithAnimationDelayd(promoById.promoBonuses.goldAmount, InAppShopUI.instance.promoRewardCotrollerUI.SetRewardUI("gold", promoById.promoBonuses.goldAmount), false));
				}
				promoById.promoBonuses.GetPromoMarketBonusesIds().ForEach(delegate(string productId)
				{
					InAppShopUI.instance.promoRewardCotrollerUI.SetRewardUI(productId, 0);
					MarketController.Instance.BuyProduct(productId, true);
				});
			}
			else
			{
				if (promoById.promoBonuses.сashAmountAlternative > 0)
				{
					CoroutineRunner.Instance.StartCoroutine(settings.ShowSpecialLabelWithAnimationDelayd(promoById.promoBonuses.сashAmountAlternative, InAppShopUI.instance.promoRewardCotrollerUI.SetRewardUI("cash", promoById.promoBonuses.сashAmountAlternative), false));
				}
				if (promoById.promoBonuses.goldAmountAlternative > 0)
				{
					CoroutineRunner.Instance.StartCoroutine(settings.ShowLabelWithAnimationDelayd(promoById.promoBonuses.goldAmountAlternative, InAppShopUI.instance.promoRewardCotrollerUI.SetRewardUI("gold", promoById.promoBonuses.goldAmountAlternative), false));
				}
			}
		}
		else
		{
			coinShopManager.OpenTabGold();
		}
	}

	private void FindInParent(Transform curObj)
	{
		if (curObj != null)
		{
			InAppProduct component = curObj.gameObject.GetComponent<InAppProduct>();
			if (component != null)
			{
				curProductItem = component;
			}
			else
			{
				FindInParent(curObj.parent);
			}
		}
	}
}
